package com.cssw.fyzb.core.dao.video;

import com.cssw.fyzb.core.model.dodb.CmfUserMusic;
import com.cssw.fyzb.core.model.dodb.CmfUserVideo;
import com.cssw.fyzb.core.util.StringUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.Record;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Feng Chen
 */
@Repository
public class UserVideoDaoImpl implements UserVideoDao {

    @Override
    public CmfUserVideo findById(Long vedioId) {
        return CmfUserVideo.dao.findById(vedioId);
    }

    @Override
    public Page<CmfUserVideo> pageByUserId(Long userId, String audit, int pageNumber, int pageSize) {
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("audit", audit);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM cmf_user_video");
        sqlParse.addSQL(" WHERE user_id = :userId");
        if (StringUtil.isNotEmpty(audit)) {
            sqlParse.addSQL(" AND audit_status = :audit");
        }
        sqlParse.addSQL(" AND state = '1'");
        sqlParse.addSQL(" order by video_id desc");
        return CmfUserVideo.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Page<CmfUserVideo> pageByAudit(String audit, int pageNumber, int pageSize) {
        Map<String, Object> param = new HashMap<>();
        param.put("audit", audit);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM cmf_user_video t");
        sqlParse.addSQL(" WHERE audit_status = :audit");
        sqlParse.addSQL(" AND state = '1'");
        sqlParse.addSQL(" ORDER BY t.video_id ");
        return CmfUserVideo.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Page<CmfUserVideo> pageByAuditStatus(int status, int pageNumber, int pageSize) {
        Map<String, Object> param = new HashMap<>();
        param.put("status", status);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM cmf_user_video t");
        sqlParse.addSQL(" WHERE status = :status ");
        sqlParse.addSQL(" and isdel = 0 ");
        sqlParse.addSQL(" ORDER BY t.id ");
        return CmfUserVideo.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Page<CmfUserVideo> listByAudit(int pageNumber, int pageSize) {
        Map<String, Object> param = new HashMap<>();
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM cmf_user_video t");
        sqlParse.addSQL(" WHERE status = 0 and video_id is not null and video_id <> '' ");
        sqlParse.addSQL(" ORDER BY t.id ");
        return CmfUserVideo.dao.paginate(pageNumber, pageSize, sqlParse);
    }


    @Override
    public Page<CmfUserVideo> pageByHot(String audit, int pageNumber, int pageSize) {
        Map<String, Object> param = new HashMap<>();
        param.put("audit", audit);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM cmf_user_video t ,");
        sqlParse.addSQL(" ( select video_id from cmf_video_hot_inst t2 where t2.start_time < now() and t2.end_time > now()  group by video_id ) t3 ");
        sqlParse.addSQL(" WHERE t.video_id = t3.video_id ");
        sqlParse.addSQL(" and audit_status = :audit");
        sqlParse.addSQL(" AND state = '1'");
        sqlParse.addSQL(" ORDER BY t.video_id ");
        return CmfUserVideo.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Long add(Long userId, String title, String href, String thumb, String vodId, String lat, String lng, String city) {
//        CmfUserVideo video = new CmfUserVideo();
//        video.setUserId(userId);
//        video.setTitle(title);
//        video.setHref(href);
//        video.setThum(thumb);
//        video.setVodId(vodId);
//        video.setLat(lat);
//        video.setLng(lng);
//        video.setCity(city);
//        video.save();
//        return video.getVideoId();
        return null;
    }

    @Override
    public void audit(Long id, String videoId, Integer status) {
        CmfUserVideo video = findById(id);
        if (video != null) {
            video.setStatus(status);
            video.setNopassTime((int) (System.currentTimeMillis() / 1000));
            video.update();
        }

    }

    @Override
    public void delete(Long vedioId) {
//        CmfUserVideo video = findById(vedioId);
//        if (video != null) {
//            video.setState("0");
//            video.setUpdateTime(new Date());
//            video.update();
//        }
    }

    @Override
    public Page<CmfUserVideo> searchVideo(Map param, Integer pageNumber, Integer pageSize) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("select * from cmf_user_video where state='1'");
        sqlParse.addSQL(" and user_id=:key");
        sqlParse.addSQL(" and audit_status=:auditStatus");
        return CmfUserVideo.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    public CmfUserMusic getCmfUserMusic(int musicId) {
        return CmfUserMusic.dao.findById(musicId);
    }

    public CmfUserVideo getCmfUserVideo(Long id) {
        return CmfUserVideo.dao.findById(id);
    }

    @Override
    public Page<Record> pageByCountLikes(int pageNumber, int pageSize) {
        SqlParse sqlParse = new SqlParse(new HashMap());
        sqlParse.addSQL("select uid, sum(likes) as likes from cmf_user_video ");
        sqlParse.addSQL(" group by uid");
        return Db.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Integer countLikes(Long userId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select sum(likes) as likes from cmf_user_video where uid = ? ");
        Integer sum = Db.queryInt(sql.toString(), userId);
        return sum == null ? 0 : sum;
    }
}
